<?php 
namespace Admin\Controller;

use Admin\Model\BlogModel;
use Think\Page;

class NewBlogController extends BaseController{
    
    public function index(){
        
        $blog = M('cat');
        
        // 获取栏目数据
        $list = $blog->order('sort desc,cat_id desc')->select();
        $count = $blog->count();
        
        
        // 拼接栏目分类
        $cat_str = '';
        foreach ($list as $v){
            $cat_str .= '{ id:'.$v['cat_id'].', pId:'.$v['pid'].', name:"'.$v['cat_name'].'",src:"'.U('NewBlog/blogList',array('id'=>$v['cat_id'])).'"},';
        }
        
        // 传递数据
        $this->assign('cat_str',$cat_str);
        $this->assign('count',$count);
        
        $this->display('newblog');
    }
    // 添加博客
    public function addBlog(){
        
        $this->assign('time',time());
     
        if (IS_POST){
            $data = I('post.');
            $data['is_commont'] = I('post.is_commont',0);
            $data['content'] = $_POST['content'];
            $data['time'] = strtotime($_POST['time']);
            unset($data['id']);
            unset($data['keywords']);
            
            if (!empty($data['theme'])){
                $data['thumb'] = explode('|', $data['theme'])[1];
                $data['theme'] = explode('|', $data['theme'])[0];
            }else {
                unset($data['theme']);
            }
            
            // 获取博客描述文字
            if ($data['description']==''){
                // 从博客内容中截取
                $data['description'] = msubstr(strip_tags($_POST['content']), 190);
            }
            // 验证表单数据
            $blog = D('Blog');
            
            if (!$date = $blog->create()){
                
                $this->error($blog->getError());die;
            }
            
            if (I('post.id')==0){// 添加数据
                $res = $blog->add($data);
                
                // 添加关键字
                if (!empty(I('post.keywords'))){
                    
                    $keys = explode(',', I('post.keywords'));
                    
                    foreach ($keys as $v){
                        // 判断关键字是否存在
                        if (!M('keys')->where('key_name="'.$v.'"')->find()){
                            $id = M('keys')->data(array('key_name'=>$v))->add();
                        }else {
                            $id = M('keys')->where('key_name="'.$v.'"')->getField('id');
                        }
                        
                        M('article_key')->data(array('art_id'=>$res,'key_id'=>$id))->add();
                    }
                }
            }else {// 修改数据
                
                if (!empty($data['theme'])){
                    
                    // 删除旧图片
                    $theme = M('blog')->where(array('id'=>I('post.id')))->getField('theme');
                    $thumb = M('blog')->where(array('id'=>I('post.id')))->getField('thumb');
                    @unlink($_SERVER['DOCUMENT_ROOT'].$theme);
                    @unlink($_SERVER['DOCUMENT_ROOT'].$thumb);
                }
                
                // 更新数据
                $res = $blog->where('id='.I('post.id'))->save($data);
                
                // 删除原有的关键字
                M('article_key')->where('art_id='.I('post.id'))->delete();
                
                // 添加关键字
                if (!empty(I('post.keywords'))){
                    
                    $keys = explode(',', I('post.keywords'));
                    
                    foreach ($keys as $v){
                        // 判断关键字是否存在
                        if (!M('keys')->where('key_name="'.$v.'"')->find()){
                            $id = M('keys')->data(array('key_name'=>$v))->add();
                        }else {
                            $id = M('keys')->where('key_name="'.$v.'"')->getField('id');
                        }
                        
                        M('article_key')->data(array('art_id'=>I('post.id'),'key_id'=>$id))->add();
                    }
                }
                
            }
            
            if ($res !== false){
                $this->show('<script>var index = parent.layer.getFrameIndex(window.name);alert("成功");parent.location.reload();parent.layer.close(index);</script>');
            }else {
                $this->error('添加数据失败');
            }
        }elseif (!empty(I('get.blog_id'))){
            
            // 获取栏目名称
            $cat_name = M('cat')->where('cat_id='.I('get.id'))->getField('cat_name');
            
            $this->assign('cat_name',$cat_name);
            
            
            // 获取博客数据
            $blog = M('blog');
            $res = $blog->where('id='.I('get.blog_id'))->find();
            
            // 获取博客栏目
            $cats = M('cat')->field(array('cat_id','cat_name'))->select();
            
            $this->assign('cats',$cats);
            $this->assign('blog',$res);
            
            $this->display('blogadd');
        }else {
            // 获取栏目名称
            $cat_name = M('cat')->where('cat_id='.I('get.id'))->getField('cat_name');
            
            $this->assign('cat_name',$cat_name);
            
            // 获取博客栏目
            $cats = M('cat')->field(array('cat_id','cat_name'))->select();
            
            $this->assign('cats',$cats);
            
            $this->display('blogadd');
        }
    }
    
    // 显示博客列表
    public function blogList(){
        
        $id = I('id');
        $where['cat_id'] = $id; 
        $blog_name = I('get.blog_name');
        $logmin= I('get.logmin');
        $logmax= I('get.logmax');
        
        // 设置博客搜索
        if (!empty($blog_name)){
            $where['title'] = array('like',"%$blog_name%");
        }
        if (!empty($logmin)){
            $timemin = strtotime($logmin);
            if (!empty($logmax)){
                $timemax = strtotime($logmax) + 3600*24;
                
                $where['time'] = array(array('lt',$timemax),array('gt',$timemin));
            }else {
                
                $where['time'] = array('gt',$timemin);
            }
        }else {
            if (!empty($logmax)){
                $timemax = strtotime($logmax) + 3600*24;
                $where['time'] = array('lt',$timemax);
            }
        }
        
        if ($id != ''){
            
            // 实现分页效果
            $count = M('blog')->where($where)->count();
            $page = new Page($count,2);
            $page->setConfig('prev', '上一页');
            $page->setConfig('next', '下一页');
            
            $show = $page->show();
            
            // 获取当前栏目下的博客数据
            $blogs = M('blog')->where($where)->order('id desc')->limit($page->firstRow.','.$page->listRows)->select();
            
            // 传递数据
            $this->assign('show',$show);
            $this->assign('totalPages',$page->totalPages);
            $this->assign('blogs',$blogs);
        }
        
        
        $this->assign('id',$id);
        
        $this->display('bloglist');
    }
    
    // 栏目搜索
    public function search(){
        
        $cat_name = trim(I('cat_name'));
        
        $cat = M('cat');
        $res = '';
        
        if ($cat_name!=''){
            $list = $cat->where(array('cat_name'=>array('like',"%$cat_name%")))->field('cat_id,cat_name')->select();
            
            foreach ($list as $v){
                $res .= '<li><a href="'.U('bloglist',array('id'=>$v['cat_id'])).'">'.$v['cat_name'].'</a></li>';
            }
        }
        echo $res;die;
    }
   
    // 删除博客
    public function delete(){
        
        $res = M('blog')->where('id='.I('get.blog_id'))->delete();
        if ($res){
            
            // 删除当前博客关联的关键字
            M('article_key')->where(array('art_id'=>I('get.blog_id')))->delete();
            
            echo 1;die;
        }else {
            echo 0;die;
        }
    }
    
}


?>